﻿using System;
using System.Collections.Generic;
using System.Xml;
using ARSoft.Tools.Net.Dns;
using System.Net;
namespace simpleDns
{
    public class DOMAIN
    {
        public bool enable;
        public string info;
        public Dictionary<string, DNSRECORD> dnsRecords;
        public IPAddress[] dnsIpSort;
        public bool dnsMode;
        public string name;
        public Dictionary<string, ARECORD> aRecords;
        private void init()
        {
            aRecords = new Dictionary<string, ARECORD>();
            enable = true;
            dnsRecords = new Dictionary<string, DNSRECORD>();
            dnsIpSort = new IPAddress[0];
            dnsMode = false;
        }
        public DOMAIN(string domain)
        {
            init();
            name = domain;
        }
        public DOMAIN()
        {
            init();
        }
        public void loadFromFile(string path)
        {
            try
            {
                XmlDocument xd = new XmlDocument();
                xd.Load(path);
                XmlNode root = xd.SelectSingleNode("root");
                name = root.SelectSingleNode("name").Attributes["value"].Value;
                info = root.SelectSingleNode("info").Attributes["value"].Value;
                enable = root.SelectSingleNode("enable").Attributes["value"].Value.Equals("1");
                dnsMode = root.SelectSingleNode("dnsMode").Attributes["value"].Value.Equals("1");
                XmlNodeList xmlDns = root.SelectNodes("dnsServer");
                foreach (XmlNode xn in xmlDns)
                {
                    DNSRECORD rec = new DNSRECORD();
                    rec.id = xn.Attributes["id"].Value;
                    rec.name = xn.Attributes["name"].Value;
                    rec.info = xn.Attributes["info"].Value;
                    rec.enable = xn.Attributes["enable"].Value.Equals("1");
                    rec.sort =Convert.ToInt16(xn.Attributes["sort"].Value);
                    dnsRecords.Add(rec.id, rec);
                }
                XmlNodeList xmlRec = root.SelectNodes("record");
                foreach (XmlNode xn in xmlRec)
                {
                    ARECORD rec = new ARECORD();
                    rec.id = xn.Attributes["id"].Value;
                    rec.name = xn.Attributes["name"].Value;
                    rec.value = xn.Attributes["value"].Value;
                    rec.info = xn.Attributes["info"].Value;
                    rec.enable = xn.Attributes["enable"].Value.Equals("1");
                    rec.type = (RecordType)Convert.ToInt16(xn.Attributes["type"].Value);
                    aRecords.Add(rec.id, rec);
                }
            }
            catch
            {
                throw new Exception("数据文件格式错误：" + path);
            }
        }
        public void saveToFile()
        {
            try
            {
                string p = string.Format(@"{0}\{1}.dns", settings.dataPath, name);
                XmlDocument xd = new XmlDocument();
                XmlNode root = xd.CreateElement("root");
                XmlElement xname = xd.CreateElement("name");
                xname.SetAttribute("value", name);
                root.AppendChild(xname);
                XmlElement xinfo = xd.CreateElement("info");
                xinfo.SetAttribute("value", info);
                root.AppendChild(xinfo);
                XmlElement xdnsMode = xd.CreateElement("dnsMode");
                xdnsMode.SetAttribute("value", dnsMode ? "1" : "0");
                root.AppendChild(xdnsMode);
                XmlElement xenable = xd.CreateElement("enable");
                xenable.SetAttribute("value", enable ? "1" : "0");
                root.AppendChild(xenable);
                foreach (ARECORD kv in aRecords.Values)
                {
                    XmlElement xrecord = xd.CreateElement("record");
                    xrecord.SetAttribute("name", kv.name);
                    xrecord.SetAttribute("id", kv.id);
                    xrecord.SetAttribute("value", kv.value);
                    xrecord.SetAttribute("type",((int)kv.type).ToString());
                    xrecord.SetAttribute("info", kv.info);
                    xrecord.SetAttribute("enable", kv.enable ? "1" : "0");
                    root.AppendChild(xrecord);
                }
                foreach (DNSRECORD ip in dnsRecords.Values)
                {
                    XmlElement xdns = xd.CreateElement("dnsServer");
                    xdns.SetAttribute("name", ip.name);
                    xdns.SetAttribute("info", ip.info);
                    xdns.SetAttribute("id", ip.id);
                    xdns.SetAttribute("sort", ip.sort.ToString());
                    xdns.SetAttribute("enable", ip.enable?"1":"0");
                    root.AppendChild(xdns);
                }
                xd.AppendChild(root);
                xd.Save(p);
            }catch(Exception ex)
            {
                throw ex;
            }
            
        }
        public void sortDns()
        {
            
            DNSRECORD[] dnsRecordsSort = new DNSRECORD[dnsRecords.Count];
            dnsRecords.Values.CopyTo(dnsRecordsSort, 0);
            int i, j;
            DNSRECORD tmp;
            bool done = false;
            j = 1;
            while(j< dnsRecordsSort.Length && !done)
            {
                done = true;
                for(i=0;i<dnsRecordsSort.Length- j; i++)
                {
                    if (dnsRecordsSort[i].sort < dnsRecordsSort[j].sort)
                    {
                        done = false;
                        tmp = dnsRecordsSort[i];
                        dnsRecordsSort[i] = dnsRecordsSort[i + 1];
                        dnsRecordsSort[i + 1] = tmp;
                    }
                }
                j++;
            }
            dnsIpSort =new IPAddress[dnsRecordsSort.Length];
            for(int x = 0; x < dnsRecordsSort.Length; x++)
            {
                dnsIpSort[x] =IPAddress.Parse( dnsRecordsSort[x].name);
            }
        }

        public void addARecord(ARECORD ar)
        {
            aRecords.Add(ar.id,ar);
        }
        public void editARecord(ARECORD ar)
        {
            aRecords[ar.id] = ar;
        }
        public void delArecord(string id)
        {
            aRecords.Remove(id);
        }

        public void addDNSRecord(DNSRECORD dr)
        {
            dnsRecords.Add(dr.id,dr);
        }
        public void editDNSRecord(DNSRECORD dr)
        {
            dnsRecords[dr.id] = dr;
        }
        public void delDNSRecord(string id)
        {
            dnsRecords.Remove(id);
        }
    }
    public class ARECORD
    {
        public string name;
        public string value;
        public string info;
        public bool enable;
        public RecordType type;
        public string id;
        public ARECORD()
        {
            id = Guid.NewGuid().ToString();
        }
    }
    public class DNSRECORD
    {
        public string name;
        public int sort;
        public bool enable;
        public string info;
        public string id;
        public DNSRECORD()
        {
            id = Guid.NewGuid().ToString();
        }
    }
}
